#include "Item.hpp"

Item::Item(App* appPtr) : Entity(appPtr)
{
	m_anim = new Animation();
	m_owner = NULL;
    SetLoaded(false);
}

Item::Item(const Item &item)
{
    // Copy ID
    SetID(item.ID());
    // Copy Anim
    SetAnimation(new Animation(*item.GetAnimation()));
}

/////////////////////////
/////// Methode Get /////
bool Item::CanBeTaken()
{
	return m_canBeTaken;
}

bool Item::IsTaken()
{
	return m_isTaken;
}

Creature* Item::Owner()
{
	return m_owner;
}

Animation* Item::GetAnimation() const
{
	return m_anim;
}

/////////////////////////
/////// Methode Set /////

void Item::SetCanBeTaken(bool canBeTaken)
{
	m_canBeTaken = canBeTaken;
}

void Item::SetIsTaken(bool isTaken)
{
	m_isTaken = isTaken;
}

void Item::SetAnimation(Animation* anim)
{
	m_anim = anim;
}

void Item::SetOwner(Creature *owner)
{
	m_owner = owner;
}

void Item::AddFlag(ItemFlag flag)
{
	m_flags.push_back(flag);
}

void Item::RemoveFlag(ItemFlag flag)
{
	m_flags.remove(flag);

}

bool Item::FindFlag(ItemFlag flag)
{
	for(std::list<ItemFlag>::iterator it = m_flags.begin(); it != m_flags.end(); it++)
	{
		if(*it == flag)
			return true;
	}
	return false;
}

void Item::LoadScript(const std::string &filename)
{

    // Get Data Script
    std::queue<TagValues> values;
    FileParser::AllValues(filename, values);

    // Initialize resource
    ImageRessource *_resource = NULL;

    while(values.size() != 0)
    {

        if(values.front().first == "ID")
            SetID(FileParser::ToInt(values.front().second.at(0)));
        else if(values.front().first == "Anim.Load")
            _resource = GetApp()->m_ressourceManager.AddImage("Anim_" + FileParser::ToString(ID()), values.front().second.at(0));
        else if(values.front().first == "Anim.Delay")
            m_anim->SetFrameTime(FileParser::ToFloat(values.front().second.at(0)));
        else if(values.front().first == "Anim.GoBack")
            m_anim->SetGoBack(static_cast<bool>(FileParser::ToInt(values.front().second.at(0))));
        else if(values.front().first == "Anim.AddFrame")
        {
            if(values.front().second.size() == 4)
            {
                sf::IntRect subRect;
                subRect.Left = FileParser::ToInt(values.front().second.at(0));
                subRect.Top = FileParser::ToInt(values.front().second.at(1));
                subRect.Right = FileParser::ToInt(values.front().second.at(2));
                subRect.Bottom = FileParser::ToInt(values.front().second.at(3));

                Frame* frame = new Frame(_resource, subRect);
                m_anim->AddFrame(frame);
            }
        }

        values.pop();
    }

    std::cout << "Script : " << filename << " loaded ! " << std::endl;

}
